package com.knife.bbs.action;

import com.knife.bbs.business.ActiveUser;
import com.knife.bbs.business.BBSRights;
import com.knife.bbs.business.SkinService;
import com.knife.bbs.object.Skin;
import com.knife.util.CommUtil;
import com.knife.web.ActionContext;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;

/**
 * 
 * <p>
 * Title:BBS风格处理类
 * </p>
 * <p>
 * Description: BBS目录的列表处理Action,使用KnifeWeb. <br>
 * 通过继承com.knife.web.tools.AbstractCmdAction,实现简单的命令式Action处理,去除了复杂烦锁的if else语句
 * </p>
 */
public class SkinAction extends BBSAction {
	private SkinService skinService = SkinService.getInstance();

	public Page doInit(WebForm form, Module module) {
		return doMenu(form, module);
	}

	public Page doChange(WebForm form, Module module) {
		String skin = CommUtil.null2String(form.get("skin"));
		Skin skobj = skinService.getSkin(skin);
		Skin oldsk = skinService.getUsedSkin();
		if (oldsk != null) {
			oldsk.setStatus(0);
			skinService.update(oldsk);
		}
		skobj.setStatus(1);
		skinService.update(skobj);
		return null;
	}

	/**
	 * 显示菜单
	 * 
	 * @param form
	 * @param module
	 * @return 菜单Page
	 */
	public Page doMenu(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		doQuery(form, module);
		if (BBSRights.checkActiveUser(user)) {
			form.addResult("activeuser", "1");
		}
		if (BBSRights.checkAdministrator(user)) {
			form.addResult("administrator", "1");
		}
		return module.findPage("/"+skin+"/", "menu");
	}

	/**
	 * 增加新皮肤
	 * 
	 * @param form
	 * @param module
	 * @return 目录编辑Page
	 */
	public Page doNew(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		if (!BBSRights.checkAdministrator(user))
			return new Page("popedomError", "norights.htm", "html", "/web/"+skin+"/");
		return module.findPage("/"+skin+"/", "edit");
	}

	/**
	 * 目录列表
	 * 
	 * @param form
	 * @param module
	 * @return 列表Page
	 */
	public Page doList(WebForm form, Module module) {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		if (!BBSRights.checkAdministrator(user))
			return new Page("popedomError", "norights.htm", "html", "/web/"+skin+"/");
		return doQuery(form, module);
	}

	/**
	 * 增选皮肤添加操作
	 * 
	 * @param form
	 * @param module
	 * @return 添加成功返回列表Page,失败返回编辑Page
	 */
	public Page doAdd(WebForm form, Module module) {
		com.knife.bbs.domain.Skin dir = new com.knife.bbs.domain.Skin();
		form.toPo(dir);
		dir.setStatus(new Integer(0));
		boolean op = skinService.add(dir);
		if (op) {
			form.addResult("msg", "皮肤添加成功！");
		} else {
			form.addResult("msg", "皮肤添加失败");
		}
		return doQuery(form, module);
	}

	/**
	 * 修改皮肤操作
	 * 
	 * @param form
	 * @param module
	 * @return 操作成功返回列表Page,失败返回编辑Page
	 */
	public Page doUpdate(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		Skin dir = skinService.getSkin(cid);
		if (dir != null) {
			// 在Action中检测权限
			if (!BBSRights.checkRights(dir, "update", user)) {
				return new Page("popedomError", "norights.htm", "html",
						"/"+skin+"/");
			}
			// 以下执行表单对象VO到PO转换的代码,在验证框架中可以省略
			com.knife.bbs.domain.Skin adir = new com.knife.bbs.domain.Skin();
			form.toPo(adir);
			skinService.update(adir);
		} else {
			form.addResult("msg", "所要修改的目录不存在！");
		}
		return doQuery(form, module);
	}

	/**
	 * 编辑数据,从持久层读取数据显示到VO层
	 * 
	 * @param form
	 * @param module
	 * @return 返回编辑Page
	 */
	public Page doEdit(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("id"));
		ActiveUser user = getCurrentUser();
		Skin dir = skinService.getSkin(cid);
		if (!BBSRights.checkRights(dir, "edit", user)) {
			return new Page("popedomError", "norights.htm", "html", "/web/"+skin+"/");
		}
		if (dir != null) {
			form.addPo(dir);
		}
		return module.findPage("/"+skin+"/", "edit");
	}

	/**
	 * 目录删除操作,根据主键cid删除相应目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回目录列表页
	 */
	public Page doDel(WebForm form, Module module) {
		String cid = CommUtil.null2String(form.get("cid"));
		ActiveUser user = getCurrentUser();
		Skin dir = skinService.getSkin(cid);
		if (dir != null) {
			if (!BBSRights.checkRights(dir, "del", user)) {
				return new Page("popedomError", "norights.htm", "html",
						"/"+skin+"/");
			}
			com.knife.bbs.domain.Skin adir = new com.knife.bbs.domain.Skin();
			skinService.del(adir);
			form.addResult("msg", "删除成功！");
		}
		return doQuery(form, module);
	}

	/**
	 * 列出所有目录
	 * 
	 * @param form
	 * @param module
	 * @return 返回列表Page
	 */
	public Page doQuery(WebForm form, Module module) {
		form.addResult("list", skinService.getSkins());
		return module.findPage("/"+skin+"/", "list");
	}

	/**
	 * 通过Session取得当前操作用户
	 * 
	 * @return ActiveUser
	 */
	public ActiveUser getCurrentUser() {
		ActiveUser user = (ActiveUser) ActionContext.getContext().getSession()
				.getAttribute("bbsuser");
		return user;
	}
}
